import uuid
from typing import Any
import pytz
from fastapi import APIRouter, HTTPException
from sqlmodel import select, func
from app.api.deps import CurrentUser, SessionDep
from uuid import UUID, uuid4
from datetime import datetime
from sqlmodel import SQLModel, Field, Relationship
from pydantic import BaseModel
from app.models.user_models import User  # 确保导入User模型
from typing import Optional, List


# -----------------------------
# 数据模型定义
# -----------------------------

class CommunityPostBase(SQLModel):
    title: str
    content: str

class CommunityPostCreate(CommunityPostBase):
    pass

class CommunityPostUpdate(SQLModel):
    title: str | None = None
    content: str | None = None

class CommunityPost(CommunityPostBase, table=True):
    __tablename__ = "community_posts"
    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
    user_id: uuid.UUID = Field(foreign_key="users.id", nullable=False)
    created_at: datetime = Field(default_factory=lambda: datetime.now(pytz.timezone('Asia/Shanghai')))

    user: User | None = Relationship()
    comments: list["Comment"] = Relationship(back_populates="post")
    likes: int = Field(default=0, description="帖子点赞数")

class CommunityPostPublic(CommunityPostBase):
    id: uuid.UUID
    user_id: uuid.UUID
    created_at: datetime
    user: User | None = None  # 添加用户信息字段


class CommunityPostsPublic(SQLModel):
    data: list[CommunityPostPublic]
    count: int

# -----------------------------
# 评论（Comment）模型
# -----------------------------

class CommentBase(SQLModel):
    content: str

class CommentCreate(CommentBase):
    post_id: uuid.UUID

class Comment(CommentBase, table=True):
    __tablename__ = "comments"
    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
    post_id: uuid.UUID = Field(foreign_key="community_posts.id", nullable=False)
    user_id: uuid.UUID = Field(foreign_key="users.id", nullable=False)
    created_at: datetime = Field(default_factory=datetime.utcnow)

    post: CommunityPost = Relationship(back_populates="comments")
    user: User | None = Relationship()
    likes: int = Field(default=0, description="评论点赞数")

class CommentPublic(CommentBase):
    id: uuid.UUID
    post_id: uuid.UUID
    user_id: uuid.UUID
    created_at: datetime
    user: User | None
    likes: int = Field(default=0, description="帖子点赞数")

class PostLike(SQLModel, table=True):
    __tablename__ = "post_likes"

    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
    post_id: uuid.UUID = Field(foreign_key="community_posts.id")
    user_id: uuid.UUID = Field(foreign_key="users.id")
    created_at: datetime = Field(default_factory=datetime.utcnow)

    class Config:
        arbitrary_types_allowed = True


class CommentLike(SQLModel, table=True):
    __tablename__ = "comment_likes"

    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
    comment_id: uuid.UUID = Field(foreign_key="comments.id")
    user_id: uuid.UUID = Field(foreign_key="users.id")
    created_at: datetime = Field(default_factory=datetime.utcnow)

    class Config:
        arbitrary_types_allowed = True


class LikeResponse(BaseModel):
    liked: bool
    likes_count: int